import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @作者 ： opentestcase024 @时间： 2023/4/13
 * @测试点: 打开快速负载均衡，同一集群，2个url连接
 **/
public class JDBC_QuikAutoBalance_Case0025 {
	public static void main(String[] args) throws Exception {
		// start前，两个url其中一个节点异常
		System.out.println("----------start----------");
		List<Connection> connections = new ArrayList<>();
		System.out.println("----------进行12个建连----------");
		String url1 = "jdbc:postgresql://" + args[0] + "/" + args[1] + "?" + args[8];
		for (int i = 0; i < 12; i++) {
			try {
				Connection connection1 = DriverManager.getConnection(url1, args[2], args[3]);
				connections.add(connection1);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		System.out.println("----------进行18个建连----------");
		String url2 = "jdbc:postgresql://" + args[4] + "/" + args[5] + "?" + args[9];
		for (int i = 0; i < 18; i++) {
			try {
				Connection connection1 = DriverManager.getConnection(url2, args[6], args[7]);
				connections.add(connection1);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		System.out.println("----------sleep 75s----------");
		TimeUnit.SECONDS.sleep(75);

		System.out.println("----------再次进行12个建连----------");
		for (int i = 0; i < 12; i++) {
			try {
				Connection connection1 = DriverManager.getConnection(url1, args[2], args[3]);
				connections.add(connection1);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		System.out.println("----------再次进行18个建连----------");
		for (int i = 0; i < 18; i++) {
			try {
				Connection connection1 = DriverManager.getConnection(url2, args[6], args[7]);
				connections.add(connection1);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		System.out.println("----------sleep 10s----------");
		TimeUnit.SECONDS.sleep(10);
		// sleep过程中，数据库实际节点查询上个操作每个节点实际连接数
		System.out.println("----------connections close start----------");
		for (Connection connection : connections) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		System.out.println("----------connections close end----------");
	}
}
